CLASS zcl_moon_cpi_api_090 DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS ZCL_MOON_CPI_API_090 IMPLEMENTATION.


  METHOD if_oo_adt_classrun~main.
    DATA: i_username TYPE c LENGTH 100,
          i_password TYPE c LENGTH 100.

    i_username = 'sb-a899779f-900a-4c6c-80f5-328c53de64c1!b384839|it!b55215'.
    i_password = '126cdc3c-e14c-4897-a584-60226fb71df9$6PDQa1mJvCgLMQ4zi8elsgLxKoVofklpQn8Op_wNMME=' .
    DATA(destination) = cl_http_destination_provider=>create_by_url( 'https://ed7d1a0dtrial.authentication.us10.hana.ondemand.com/oauth/token' ).
    DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( destination ).
    DATA(request) = http_client->get_http_request( ).



*    DATA(body) = `{'client_id': 'sb-a899779f-900a-4c6c-80f5-328c53de64c1!b384839|it!b55215', 'client_secret': '126cdc3c-e14c-4897-a584-60226fb71df9$6PDQa1mJvCgLMQ4zi8elsgLxKoVofklpQn8Op_wNMME=', 'grant_type': 'client_credentials'}`.
    request->set_header_fields( VALUE #(
          ( name = 'Content-Type'
            value = 'application/x-www-form-urlencoded' )
            ) ).

*    request->set_authorization_basic(
*      i_username = 'sb-a899779f-900a-4c6c-80f5-328c53de64c1!b384839|it!b55215'
*      i_password = '126cdc3c-e14c-4897-a584-60226fb71df9$6PDQa1mJvCgLMQ4zi8elsgLxKoVofklpQn8Op_wNMME=' ).
*    request->set_text( body ).
    request->set_form_field( i_name = 'client_id' i_value = 'sb-a899779f-900a-4c6c-80f5-328c53de64c1!b384839|it!b55215' ).
    request->set_form_field( i_name = 'client_secret' i_value = '126cdc3c-e14c-4897-a584-60226fb71df9$6PDQa1mJvCgLMQ4zi8elsgLxKoVofklpQn8Op_wNMME=' ).
    request->set_form_field( i_name = 'grant_type' i_value = 'client_credentials' ).


    DATA(token_response) = http_client->execute( if_web_http_client=>post ).
    DATA(token_str) = token_response->get_text( ).
    out->write( token_str ).
    DATA:BEGIN OF ls_token,
           access_token TYPE string,
           token_type   TYPE string,
           expires_in   TYPE string,
           scope        TYPE string,
         END OF ls_token.

    /ui2/cl_json=>deserialize( EXPORTING json = token_str CHANGING data = ls_token  ).



    DATA(destination2) = cl_http_destination_provider=>create_by_url( 'https://ed7d1a0dtrial.it-cpitrial06.cfapps.us10-001.hana.ondemand.com/api/v1/IntegrationPackages?$format=json' ).
    DATA(http_client2) = cl_web_http_client_manager=>create_by_http_destination( destination2 ).
    DATA(request2) = http_client2->get_http_request( ).

    DATA(bearer) = |Bearer { ls_token-access_token }|.

    request2->set_header_field(
          i_name  = 'Authorization'
          i_value = bearer ).

    DATA(meta_response) = http_client2->execute( if_web_http_client=>get ).
    DATA(meta_str) = meta_response->get_text( ).

    " 定义 JSON 解析器
    DATA:lr_data TYPE REF TO data.

    /ui2/cl_json=>deserialize(
     EXPORTING json = meta_str
               pretty_name = /ui2/cl_json=>pretty_mode-none
     CHANGING data = lr_data
     ).

    FIELD-SYMBOLS <fs_data> TYPE any.
    FIELD-SYMBOLS <fs_d> TYPE any.
    FIELD-SYMBOLS <fs_results> TYPE any.
    FIELD-SYMBOLS <fs_itab> TYPE any.

    ASSIGN lr_data->* TO <fs_data>.
    ASSIGN ('<fs_data>-d') TO <fs_d>.
    ASSIGN <fs_d>->* TO <fs_results>.
    ASSIGN ('<fs_results>-results') TO <fs_itab>.

    DATA:BEGIN OF ls_data,
           id           TYPE string,
           name         TYPE string,
           resourceid   TYPE string,
           description  TYPE string,
           shorttext    TYPE string,
           version      TYPE string,
           vendor       TYPE string,
           mode         TYPE string,
           modifiedby   TYPE string,
           creationdate TYPE string,
           modifieddate TYPE string,
           createdby    TYPE string,
         END OF ls_data.
    FIELD-SYMBOLS <fs_line_data> TYPE any.
    LOOP AT <fs_itab>->* ASSIGNING FIELD-SYMBOL(<fs_line>).
      ASSIGN <fs_line>->* TO <fs_line_data>.
      MOVE-CORRESPONDING <fs_line_data> to ls_data.
    ENDLOOP.


    out->write( meta_str ).
*    DATA(communication_system) = 'CPI_MOON'.
*    DATA(arrangement_factory) = cl_com_arrangement_factory=>create_instance( ).
*
*    DATA(comm_arrangement_range) = VALUE if_com_arrangement_factory=>ty_query-cs_id_range(
*      ( sign = 'I' option = 'EQ' low = communication_system ) ).
*
*    arrangement_factory->query_ca(
*      EXPORTING
*        is_query = VALUE #( cs_id_range = comm_arrangement_range )
*      IMPORTING
*        et_com_arrangement = DATA(arrangements) ).
*
*    DATA(arrangement) = arrangements[ 1 ].
*
*    DATA(destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*      comm_scenario  = 'YMOON_CS_091'
*      service_id     = 'YMOON_OUT_CPI_REST'
*      comm_system_id = arrangement->get_comm_system_id( ) ).
*
*    DATA(http_client) = cl_web_http_client_manager=>create_by_http_destination( destination ).
*    DATA(request) = http_client->get_http_request( ).
*
*    DATA(response) = http_client->execute( if_web_http_client=>get ).
*    out->write( response->get_text( ) ).

***** get destination for http client by com arrangement in steampunk
*    TRY.
*        DATA(lo_destination) = cl_http_destination_provider=>create_by_comm_arrangement(
*                                    comm_scenario  = 'YMOON_CS_092'
*                                    service_id     = 'YMOON_OS_CPI_REST'
*                                    ).
*      CATCH cx_http_dest_provider_error.
*        "handle exception
*    ENDTRY.
*
*    try.
*        data(lo_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_destination ).
*      catch cx_web_http_client_error into data(lo_ex).
*         data(text) = lo_ex->get_text( ).
*        "handle exception
*    endtry.
*    DATA(lo_request) = lo_http_client->get_http_request( ).
*
*    lo_request->set_uri_path( EXPORTING i_uri_path = |/MessageProcessingLogs('AGeQhhelJO0KffVhDy8LgZFQs_1R')/Attachments|  ).
*
*    try.
*        data(lo_response) = lo_http_client->execute( if_web_http_client=>get ).
*      catch cx_web_http_client_error.
*        "handle exception
*    endtry.
*
*
*    DATA(res)   = lo_response->get_text( ).
*    out->write( RES ).



  ENDMETHOD.
ENDCLASS.
